﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace RattlerRace
{
    public class Ball : Block
    {
        private int _direction;

        public int Direction
        {
            get { return _direction; }
            set { _direction = value; }
        }

        private bool preveriBlok(Block[,] gameGrid, int positionX, int positionY)
        {
            return ((gameGrid[P.X + positionX, P.Y + positionY] is Wall) || 
                (gameGrid[P.X + positionX, P.Y + positionY] is BodyPart) ||
                (gameGrid[P.X + positionX, P.Y + positionY] is Ball) ||
                (gameGrid[P.X + positionX, P.Y + positionY] is Apple));
        }

        public Ball(Point zacetek, int smer)
        {
            this.P = zacetek;
            this._direction = smer;
        }

        static public void premakniZogico(Block[,] gameGrid, ref List<Ball> balls)
        {
            for(int i=0; i<balls.Count; i++)
            {
                balls[i].returnNewDirection(gameGrid);
                switch(balls[i]._direction)
                {
                    case 4:
                        Point temp = balls[i].P;
                        temp.X++;
                        temp.Y--;
                        balls[i].P = temp;
                        gameGrid[balls[i].P.X, balls[i].P.Y] = balls[i];
                        gameGrid[balls[i].P.X - 1, balls[i].P.Y + 1] = new BlankBlock();
                        break;
                    case 5:
                        Point temp1 = balls[i].P;
                        temp1.X++;
                        temp1.Y++;
                        balls[i].P = temp1;
                        gameGrid[balls[i].P.X, balls[i].P.Y] = balls[i];
                        gameGrid[balls[i].P.X - 1, balls[i].P.Y - 1] = new BlankBlock();
                        break;
                    case 6:
                        Point temp2 = balls[i].P;
                        temp2.X--;
                        temp2.Y++;
                        balls[i].P = temp2;
                        gameGrid[balls[i].P.X, balls[i].P.Y] = balls[i];
                        gameGrid[balls[i].P.X + 1, balls[i].P.Y - 1] = new BlankBlock();
                        break;
                    case 7:
                        Point temp3 = balls[i].P;
                        temp3.X--;
                        temp3.Y--;
                        balls[i].P = temp3;
                        gameGrid[balls[i].P.X, balls[i].P.Y] = balls[i];
                        gameGrid[balls[i].P.X + 1, balls[i].P.Y + 1] = new BlankBlock();
                        break;
                }
            }
        }

        /// <summary>
        /// Metoda postavi novo smer glede na podane parametre oz. ponastavi žogico (če ta pride v kot
        /// </summary>
        /// <param name="gameGrid"></param>

        //POTREBEN DOGOVOR O TEM, KAM SE POSTAVI ŽOGICA V PRIMERU DA ZADANE KOT!
        public void returnNewDirection(Block[,] gameGrid) //predvidim, da so položaji stene podani isto kot smeri za žogo
        {
            switch(_direction)
            {
                case 4:
                    if (preveriBlok(gameGrid, 1, -1)) // najprej preverjam, če je do trka sploh prišlo
                    {
                        //preverjam, če je zgornja stena, desna stena ali pa kot
                        if (preveriBlok(gameGrid, 0, -1))
                        {
                            if (preveriBlok(gameGrid, 1, 0)) // kot
                            {
                                _direction = 6;
                            }
                            else //zgornja stena
                                _direction = 5;
                        }
                        //sicer je desna stena
                        else
                            _direction = 7;
                    }
                    break;
                case 5:
                    if (preveriBlok(gameGrid, 1, 1)) //torej, če imamo položaj
                    {
                        //preverjam, če je spodnja stena, desna stena ali kot
                        if (preveriBlok(gameGrid, 0, 1)) //grid.y + 1
                        {
                            if (preveriBlok(gameGrid, 1, 0)) //kot
                            {
                                _direction = 7;
                            }
                            else //spodnja stena
                                _direction = 4;
                        }
                        //sicer je desna stena
                        else
                            _direction = 6;
                    }
                    break;
                case 6:
                    if (preveriBlok(gameGrid, -1, 1))
                    {
                        //preverjam, če je spodnja stena, leva stena ali kot
                        if (preveriBlok(gameGrid, 0, 1))
                        {
                            if (preveriBlok(gameGrid, -1, 0)) //kot
                            {
                                _direction = 4;
                            }
                            else // spodnja stena
                                _direction = 7;
                        }
                        //sicer je leva stena
                        else
                            _direction = 5;
                    }
                    break;
                case 7:
                    if (preveriBlok(gameGrid, -1, -1))
                    {
                        //preverjam, če je zgornja stena, leva stena ali kot
                        if (preveriBlok(gameGrid, 0, -1))
                        {
                            if (preveriBlok(gameGrid, -1, 0)) //kot
                            {
                                _direction = 5;
                            }
                            else //zgornja stena
                                _direction = 6;
                        }
                        //sicer je leva stena
                        else
                            _direction = 4;
                    }
                    break;
            }
        }
    }
}
